<?php
function dateToDb($date){
	if(!ereg("(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([1-9]|[12][0-9]|[3][01]), ([1][9]|[2][0-9])[0-9][0-9]", $date))
		return "";
		
	$mname = array("Jan"=>"01", "Feb"=>"02", "Mar"=>"03", "Apr"=>"04", "May"=>"05", "Jun"=>"06", "Jul"=>"07", "Aug"=>"08", "Sep"=>"09", "Oct"=>"10", "Nov"=>"11", "Dec"=>"12");
		
	$month = $mname[substr($date, 0, 3)];
	$year = substr($date, -4, 4);	
	$day = substr($date, 4, 2);
	if(!ereg("^[0-9][0-9]$", $day)){	
		$day = "0$day";
	}
	
	return "$year-$month-$day";
}
/*	
$date = "Sep 13, 2000";
echo dateToDb($date);
*/
//convert date param to Mname Day, Year
function dateFromDb($date){
	if(!$date)
		return "";
	if($date == "0000-00-00")
		return "";
		
	$mname = array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
		
	$temp = split("-", $date);
	
	$year = $temp[0];
	if(ereg("^0", $temp[1]))
		$month = substr($temp[1], 1);
	else
		$month = $temp[1];
		
	$month = $mname[$month];
	
	if(ereg("^0", $temp[2]))
		$day = substr($temp[2], 1);
	else		
		$day = $temp[2];
	
	return "$month $day, $year";
}

//convert mm/dd/yy
function slashedDateToDb($date){
	if((count(split("-",$date)) == 2) && (strpos($date,'-') == 4) && (strpos($date,'-',5) == 7)){

		$split_date2=split("-",$date);
		if ($split_date2[0]<10 && strlen($split_date2[0]==1)){
			$split_date2[0]="0".$split_date2[0];
		}
		if ($split_date2[1]<10 && strlen($split_date2[1]==1)){
			$split_date2[1]="0".$split_date2[1];
			}
									         
		return $split_date2[0]."-".$split_date2[1]."-".$split_date2[2];
	}
	
	
	$split_date = split("/", $date);
	if(count($split_date) != 3)
	{
		return "";
	}	
	if(strlen($split_date[2]) == 2){
		if($split_date[2] <  70){
			$split_date[2] = "20".$split_date[2];
		}else
			$split_date[2] = "19".$split_date[2];
	}
	if ($split_date[0]<10 && strlen($split_date[0]==1)){
		$split_date[0]="0".$split_date[0];
	}
	if ($split_date[1]<10 && strlen($split_date[1]==1)){
		$split_date[1]="0".$split_date[1];
	}

	return "$split_date[2]-$split_date[0]-$split_date[1]";
}

function setMilitaryTime($time){
	//make sure that the time is in military time
	$split_time = split(":", $time);	
	$hour = $split_time[0];
	$split_time = split(" ", $split_time[1]);
	$min = $split_time[0];
	$meridian = $split_time[1];

	if(ereg("^(am|AM)$", $meridian)){
		if($hour == 12){
			$hour = 0;
		}else if($hour < 10)
			$hour = "0".$hour;
	}else if(ereg("^(pm|PM)$", $meridian)){
		if($hour < 12){
			$hour += 12;
		}
	}
	
	return $hour.":".$min;	
}
/************ Test **************/

/*
	$time = "12:30 pm";
	echo setMilitaryTime($time);
$testvars = array("empty" => "done", "nada" => "NA", "2007-07-01" => "07/01/07", "zero" => "");
foreach($testvars as $key => $value)
	echo $key.": ".slashedDateToDb($value)."<br/>";
	*/

